
package com.market.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import com.market.constant.Constant;
import com.market.web.tag.Pages;
import com.market.web.utils.ListUtils;

public class ProSearchAction extends ProductAction
{

    public String getCategory()
    {
        return category;
    }

    public void setCategory(String category)
    {
        this.category = category;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getOrderBy()
    {
        return orderBy;
    }

    public void setOrderBy(String orderBy)
    {
        this.orderBy = orderBy;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest reqeust)
    {
      /*  if (!"bowb".equals(category) && !"arrow".equals(category) && !"ball".equals(category)
                && !"collimator".equals(category) && !"safety".equals(category))
        {
            category = "bowa";
        }*/
        
        
        if(logger.isDebugEnabled())
        {
            logger.debug("category xxxxxxxxxx is"+category);
        }
            
        if (!"rice".equals(category) && !"beer".equals(category))
        {
            category = "rice";
        }
        
        if (!"hot".equals(orderBy) && !"adddate".equals(orderBy) && !"vipprice".equals(orderBy))
        {
            orderBy = "price";
        }
        return null;
    }

    public String getParameter()
    {
        return parameter;
    }

    public void setParameter(String parameter)
    {
        this.parameter = parameter;
    }

    private String action="";

    private String type="";

    private String orderBy="";

    private String name="";

    private String parameter="";

    private String category="";
    
    public String execute() throws Exception
    {
     
        try
        {
            //SearchForm sf = (SearchForm)form;
            setOtherInfo(getCategory());
            int p = getCurPage();
            //String category,String type,String name,String parameter,String orderBy
            List list = getProductService().findByForm(getCategory(),getType(),getName(),getParameter(),getOrderBy(),
                    (p - 1) * Constant.perPage, Constant.perPage);
            int count = getProductService().getCountByForm(getCategory(),getType(),getName(),getParameter(),getOrderBy());
            setPages(p, count, Constant.perPage);
            return processSearch(list, getCategory());
        }
        catch (Throwable a)
        {
            logger.error("", a);
            return null;
        }

    }
    
    
    protected void setPages( int curP, int count,
            int perPage)
    {
        Pages pages = new Pages();
        pages.setPerPage(perPage);
        pages.setRowCount(count);
        pages.setCurPage(curP);
        StringBuffer path = new StringBuffer();
        path.append(getRequest().getServletPath()).append(".do?").append("category=").append(getCategory()).append("&type=")
                .append(getType()).append("&name=").append(getName()).append("&parameter=").append(
                        getParameter()).append("&orderBy=").append(getOrderBy());
        pages.setPath(path.toString());
        getRequest().setAttribute("pages", pages);
    }


    private String processSearch(List list, String category)
    {
        String forward = null;
        
        if(logger.isDebugEnabled())
        {
           logger.debug("category is"+category);
        }
        if ("rice".equals(category))
        {
            getRequest().setAttribute("productList", ListUtils.convert(list, com.market.bean.Ball.class,
                    com.market.web.form.RiceForm.class));
            forward = "rice";
        }
        else if ("beer".equals(category))
        {
            getRequest().setAttribute("productList", ListUtils.convert(list, com.market.bean.Ball.class,
                    com.market.web.form.BeerForm.class));
            forward = "beer";
        }
        
        
      /*  else if ("ball".equals(category))
        {
            request.setAttribute("productList", ListUtils.convert(list, com.market.bean.Ball.class,
                    com.market.web.form.BallForm.class));
            forward = "ball";
        }
        else if ("bowb".equals(category))
        {
            request.setAttribute("productList", ListUtils.convert(list, com.market.bean.Bowb.class,
                    com.market.web.form.BowbForm.class));
            forward = "bowb";
        }
        else if ("arrow".equals(category))
        {
            request.setAttribute("productList", ListUtils.convert(list, com.market.bean.Arrow.class,
                    com.market.web.form.ArrowForm.class));
            forward = "arrow";
        }
        else if ("collimator".equals(category))
        {
            request.setAttribute("productList", ListUtils.convert(list, com.market.bean.Collimator.class,
                    com.market.web.form.CollForm.class));
            forward = "coll";
        }*/
        else if ("safety".equals(category))
        {
            getRequest().setAttribute("productList", ListUtils.convert(list, com.market.bean.Safety.class,
                    com.market.web.form.SafetyForm.class));
            forward = "safety";
        }
        else
        {
            getRequest().setAttribute("productList", ListUtils.convert(list, com.market.bean.Bowa.class,
                    com.market.web.form.BowaForm.class));
            forward = "rice";
        }
        return forward;
        //return mapping.findForward(forward);
    }

}
